Skip to content

Do not materialise X in [X; 0] when X is unsizing a const #145277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dingxiangfei2009
Copy link
Contributor

@dingxiangfei2009 dingxiangfei2009 commented Aug 11, 2025

Fix #143671

It turns out that MIR builder materialise X in [X; 0] into a temporary local when X is unsizing a const. This led to a confusing call to destructor of X when such a destructor is declared. Playground

This patch may miss out other cases that we should avoid materialisation in case of [X; 0]. Suggestions to include is most welcome!

@rustbot
Copy link
Collaborator

rustbot commented Aug 11, 2025

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 11, 2025
@rust-log-analyzer

This comment has been minimized.

@dingxiangfei2009 dingxiangfei2009 force-pushed the fold-coercion-into-const branch from 6a64501 to b7b9519 Compare August 11, 2025 22:54
@dingxiangfei2009
Copy link
Contributor Author

A question to @traviscross ...

Should we also need to run this through t-lang as well? It is hard to imagine that anything would rely on destructor called in this way, but I am not sure.

@traviscross traviscross added T-lang Relevant to the language team and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 11, 2025
@traviscross
Copy link
Contributor

A question to @traviscross ...

Should we also need to run this through t-lang as well? It is hard to imagine that anything would rely on destructor called in this way, but I am not sure.

Well, let's see. It looks like this issue does affect the user-visible stable behavior of the language:

use core::{mem, ptr, sync::atomic::{AtomicU64, Ordering}};

static COUNT: AtomicU64 = AtomicU64::new(0);

struct S;
impl Drop for S {
    fn drop(&mut self) {
        COUNT.fetch_add(1, Ordering::Relaxed);
    }
}
trait Tr {}
impl Tr for S {}

const X: Box<dyn Tr> = unsafe {
    mem::transmute::<_, Box<S>>(ptr::dangling_mut::<S>())
};

fn main() {
    assert_eq!(0, COUNT.load(Ordering::Relaxed));
    let _a: &'static [Box<dyn Tr>; 0] = &[X; 0];
    assert_eq!(1, COUNT.load(Ordering::Relaxed));
    //~^ Drop was run.
    let _b = [X; 0];
    assert_eq!(1, COUNT.load(Ordering::Relaxed));
    //~^ Drop wasn't run.
}

So, yes, it's ours to fix via FCP.

Anyone think we need a crater run? For my part, I could go either way on that, and I do want us to fix this, so let's:

@rfcbot fcp merge

@rustbot labels +I-lang-nominated +needs-fcp

cc @rust-lang/lang @rust-lang/lang-advisors

@rfcbot
Copy link
Collaborator

rfcbot commented Aug 11, 2025

Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Aug 11, 2025
@rustbot rustbot added I-lang-nominated Nominated for discussion during a lang team meeting. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. labels Aug 11, 2025
@rfcbot rfcbot added the disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. label Aug 11, 2025
@theemathas
Copy link
Contributor

theemathas commented Aug 12, 2025

Another fun edge case where this change is visible in user code: This code compiles with this PR, but fails on nightly due to dropping in const

use std::rc::Weak;

const X: Weak<i32> = Weak::new();
const Y: [Weak<dyn Send>; 0] = [X; 0];

@@ -656,6 +657,24 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
block.and(rvalue)
}

/// Recursively inspect a THIR expression and probe through unsizing
/// operations that can be const-folded today.
fn check_constness(&self, mut ek: &'a ExprKind<'tcx>) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/ek/kind/

I've never seen ek used for an ExprKind, kind is the normal name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Renamed.

@traviscross traviscross added the P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang label Aug 12, 2025
@nnethercote
Copy link
Contributor

The code changes seem fine, speaking as a reviewer who is not at all familiar with the MIR building and relevant lang details. The example from @theemathas might be worth adding to the test case.

@dingxiangfei2009
Copy link
Contributor Author

@bors try

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Aug 12, 2025
…<try>

Do not materialise X in [X; 0] when X is unsizing a const
@dingxiangfei2009
Copy link
Contributor Author

Okay, I will request a crater run ahead of the next meeting.

@dingxiangfei2009
Copy link
Contributor Author

I will push a commit to include the second test cases.

@rust-bors
Copy link

rust-bors bot commented Aug 12, 2025

☀️ Try build successful (CI)
Build commit: 981ca44 (981ca440e18f6cc4bc9026d4be8ddb1ca8378af7, parent: a1531335fe2807715fff569904d99602022643a7)

@dingxiangfei2009
Copy link
Contributor Author

dingxiangfei2009 commented Aug 12, 2025

Hello team. I would like to request

@ craterbot run mode=build-and-test crates=top-100

for a quick triage. 🙇

Signed-off-by: Ding Xiang Fei <[email protected]>
Co-authored-by: Theemathas Chirananthavat <[email protected]>
@nnethercote
Copy link
Contributor

Let's do the top 1000, just for more coverage :)

@craterbot run mode=build-and-test crates=top-1000

@craterbot
Copy link
Collaborator

👌 Experiment pr-145277 created and queued.
🤖 Automatically detected try build 981ca44
⚠️ Try build based on commit c1ab3ed, but latest commit is 8444dde. Did you forget to make a new try build?
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 12, 2025
@nnethercote
Copy link
Contributor

⚠️ Try build based on commit c1ab3ed, but latest commit is 8444dde. Did you forget to make a new try build?

The try build was done before the second commit was added, but it's just a test, so this shouldn't matter.

@craterbot
Copy link
Collaborator

🚧 Experiment pr-145277 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🎉 Experiment pr-145277 is completed!
📊 1 regressed and 2 fixed (1000 total)
📰 Open the summary report.

⚠️ If you notice any spurious failure please add them to the denylist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Aug 14, 2025
@dingxiangfei2009
Copy link
Contributor Author

1 regression from dbus-0.9.7 because libc-v0.2.139 was not available on crates.io.

@traviscross
Copy link
Contributor

I.e., all the regressions were spurious.

@RalfJung
Copy link
Member

Does this relate to #79580 somehow?

@nnethercote
Copy link
Contributor

r=me once the lang team has approved.

@nnethercote nnethercote added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 15, 2025
@theemathas
Copy link
Contributor

@RalfJung Yes. I'm not sure if I'm misunderstanding that issue (which seems to have an outdated description). But this PR is fixing the behavior to be correct, right?

@RalfJung
Copy link
Member

I'm not sure, I didn't page this back in -- I just remembered seeing something about empty arrays before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. I-lang-nominated Nominated for discussion during a lang team meeting. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). T-lang Relevant to the language team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unsize-coercible type causes [SOME_CONST; 0] to execute Drop, but only if type is annotated.
9 participants